package com.probiz.estoresa.system.web.action.shipping;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.system.Carrier;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.system.service.CarrierManager;

public class CarrierController extends GenericStoreAdminController<Carrier> {
	private CarrierManager carrierManager = null;

	public void setCarrierManager(CarrierManager inMgr) {
		this.carrierManager = inMgr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Carrier entity) {
		return entity.getCarrierName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = carrierManager;
	}
	
	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("2", "convert_gbk(carrierName)");
		sortMap.put("3", "websiteUrl");
		sortMap.put("4", "carrierAddress");
		sortMap.put("5", "linkman");
		sortMap.put("6", "telephone");
		return sortMap;
	}

	@Override
	protected void preSave(HttpServletRequest request, Carrier entity, BindException errors) {
		
	}
	
	
	/**
	 * 删除一条记录。并在应用级出错的时候显示出错信息。
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 取得Form对应的Model。因为要先保存其名称，否则可能会连I18N数据也已经删除了；另外可以用于出错的时候恢复显示Form页面
		Carrier entity = formBackingObject(request);
		boolean flag;
		// 删除并设置信息
		String entityId = request.getParameter(entityIdName);
		if (!StringUtils.isEmpty(entityId)) {
			// 先构造提示信息，否则信息对应的记录可能也会被删除。
			String entityName = getEntityName(entity);
			try {
				flag = carrierManager.deleteFully(entity);
			} catch (DataIntegrityViolationException e) {
				e.printStackTrace();
				saveErrorMessage(request, this.getMessage("common.message.delete.constraintViolation"));
				
				// 为了出错的时候可以恢复表单显示，构造errors。无须作表单绑定和验证
				ServletRequestDataBinder binder = this.getDefaultBinder(request,entity);
				BindException errors = new BindException(binder.getBindingResult());
				
				return showForm(request, errors);
			}
			if(flag){
				postDelete(request, entityName);
			}else{
				String message = getMessage("carrier.delete.false", new Object[] {entityName });
				saveMessage(request, message);
				return getRedirectToActionView("edit", ((BaseObject) entity).getId()
						.toString());
			}
		} else {
			saveMessage(request, getMessage("errors.invalid.delete.id",	new Object[] { entityId }));
		}
		
		return getModelAndView(cancelFormView, null);
	}
	
	
	/**
	 * 批量删除记录。批量处理的模式：应该重用单个实体处理的方法，否则，就会出现处理不一致的情况。本模式能保证事务完整性。成功后重定向到列表页面，以消除重复提交问题。
	 * 
	 * @param request
	 * @param response
	 */
	public ModelAndView multiDelete(HttpServletRequest request,
			HttpServletResponse response) {
		// 取得要删除的记录的id的列表。注意：选择框的id要是"multiIds"。
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for(int i=0; i<ids.length; i++){
				Carrier entity = carrierManager.getById(Integer.valueOf(ids[i]));
				String entityName = getEntityName(entity);
				boolean flag = false;
				try {
					flag = carrierManager.deleteFully(entity);
				} catch (DataIntegrityViolationException e) {
					this.saveErrorMessage(request, this.getMessage("common.message.delete.constraintViolation"));
				}
				if(flag){
					postDelete(request, entityName);
				}else{
					String message = getMessage("carrier.delete.false", new Object[] {entityName });
					saveMessage(request, message);
				}
				
			}
		}
		return getRedirectToActionView("search");
	}
	


}
